package com.gggd.aopdemo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author srx
 * @date 2021/12/24 22:57
 */
@Aspect
@Component
@Slf4j
public class AfterThrowingDemo {
    /**
     * 定义一个切面，拦截 club.gggd.controller 包下的所有方法
     */
    @Pointcut("@annotation(com.gggd.aopdemo.anno.MyAnnotation)")
    public void pointCut() {}

    /**
     * 在上面定义的切面方法执行抛异常时，执行该方法
     * @param joinPoint jointPoint
     * @param ex ex
     */
    @AfterThrowing(pointcut = "pointCut()", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Throwable ex) {
        Signature signature = joinPoint.getSignature();
        String method = signature.getName();
        // 处理异常的逻辑
        log.info("执行方法{}出错，异常为：{}", method, ex.toString());
    }
}
